var colors=['#E54912','#497B36','#42BABC','#DD7C6A','#F4BE29','#BCBE48','#B5DBBA','#ABB7D0','#8595CA','#D7A2C6'];
var labelTop = {
	    normal : {
	        label : {
	            show : true,
	            position : 'center',
	            formatter : '{b}',
	            textStyle: {
	                baseline : 'bottom'
	               
	            }
	        },
	        labelLine : {
	            show : false
	        }
	    }
	};
	var labelFromatter = {
	    normal : {
	        label : {
	            formatter : function (params){
	                return (100 - params.value).toFixed(2)
	            },
	            textStyle: {
	                baseline : 'top'
	            }
	        }
	    },
	}
	var labelBottom = {
	    normal : {
	        color: '#ccc',
	        label : {
	            show : true,
	            position : 'center',
	            textStyle: {
	               	color:'red'
	            }
	        },
	        labelLine : {
	            show : false
	        }
	    },
	    emphasis: {
	        color: 'rgba(33,33,33,33)'
	    }
	};
var ECharts={
	ChartDataFormate: {
        FormateNOGroupData: function (data) {//data的格式如上的Result1，这种格式的数据，多用于饼图、单一的柱形图的数据源
            var categories = [];
            var datas = [];
            for (var i = 0; i < data.length; i++) {
                categories.push(data[i].name || "");
                datas.push({ name: data[i].name, value: data[i].value || 0 });
            }
            return { category: categories, data: datas };
        },
        FormateData: function (data) {//data的格式如上的Result1，这种格式的数据，多用于饼图、单一的柱形图的数据源
            var categories = [];
            var datas = [];
            var total = 0;
            for (var i = 0; i < data.length; i++) {
                categories.push(data[i].name || "");
                datas.push(data[i].value || 0 );
                total+=parseFloat(data[i].value);
            }
            return { category: categories, data: datas,total:total};
        },
        FormateData: function (data) {//data的格式如上的Result1，这种格式的数据，多用于饼图、单一的柱形图的数据源
            var categories = [];
            var datas = [];
            for (var i = 0; i < data.length; i++) {
                categories.push(data[i].name || "");
                datas.push(parseFloat(data[i].value));
            }
            return { category: categories, data: datas };
        },
        FormateGroupData: function (data, type, is_stack) {//data的格式如上的Result2，type为要渲染的图表类型：可以为line，bar，is_stack表示为是否是堆积图，这种格式的数据多用于展示多条折线图、分组的柱图
            var chart_type = 'line';
            if (type)
                chart_type = type || 'line';
            var xAxis = [];
            var group = [];
            var series = [];
            var highSeries = [];
            var map = new Map();
            var indicator = [];
            for (var i = 0; i < data.length; i++) {
                for (var j = 0; j < xAxis.length && xAxis[j] != data[i].name; j++);
                if (j == xAxis.length)
                    xAxis.push(data[i].name);
                for (var k = 0; k < group.length && group[k] != data[i].group; k++);
                if (k == group.length)
                    group.push(data[i].group);
            }
            if (chart_type =='radar') {
            	for (var i = 0; i < xAxis.length; i++) {
            		 var series_temp = { text: xAxis[i], max: 100};
            		indicator.push(series_temp);
				}
			}
            for (var i = 0; i < group.length; i++) {
            	var temp = [];
            	var hdata = []
            	var total = 0;
            	for(var m = 0; m < xAxis.length;m++){
            	for (var j = 0; j < data.length; j++) {
            			if((group[i]==data[j].group)&&(xAxis[m] ==data[j].name)){
            				temp.push(data[j].value);
            				total+=parseFloat(data[j].value);
            			}
            		}
            	if (temp.length==m) {
					temp.push(0);
				}
            	}
            	hdata.push(group[i]);
            	hdata.push(total);
                switch (type) {
                    case 'bar':
                        var series_temp = { name: group[i], data: temp, type: chart_type,barWidth:30};
                        if (is_stack)
                            series_temp = $.extend({}, { stack: 'stack' }, series_temp);
                        break;
                    case 'map':
                        var series_temp = {
                            name: group[i], type: chart_type, mapType: 'china', selectedMode: 'single',
                            itemStyle: {
                                normal: { label: { show: true} },
                                emphasis: { label: { show: true} }
                            },
                            data: temp
                        };
                        break;
                    case 'line':
                        var series_temp = { name: group[i], data: temp, type: chart_type };
                        if (is_stack)
                            series_temp = $.extend({}, { stack: 'stack' }, series_temp);
                        break;
                    case 'radar':
                        var series_temp = { name: group[i], value: temp};
                        break;
                    default:
                        var series_temp = { name: group[i], data: temp, type: chart_type };
                }
                series.push(series_temp);
                highSeries.push(hdata);
            }
            if (chart_type =='radar') {
            	return { category: group, indicator: indicator, series: series};
			}else{
				return { category: group, xAxis: xAxis, series: series,highSeries:highSeries};
			}
        },
        GetRoadMapData: function (data) {
        	 var group = [];
        	 var xAxis = [];
             var xData = [];
             var xtotal =[];
             var map = new Map();
             var forward = [];
             for (var i = 0; i < data.length; i++) {
                 for (var j = 0; j < xAxis.length && xAxis[j] != data[i].name; j++);
                 if (j == xAxis.length)
                     xAxis.push(data[i].name);
                 for (var k = 0; k < group.length && group[k] != data[i].group; k++);
                 if (k == group.length)
                     group.push(data[i].group);
                 for (var m = 0; m < forward.length && forward[m] != data[i].forward; m++);
                 if (m == forward.length)
                	 forward.push(data[i].forward);
             }
             for (var i = 0; i < xAxis.length; i++) {
            	 for (var j = 0; j < data.length; j++) {
            		 if (xAxis[i] == data[j].name);
            		   xtotal.push(data[j].alone);
            		   break;
            	 }
			}
              for(var c=0;c<xAxis.length;c++){
            	 for(var m=0;m<forward.length;m++){
            		 var xdata=[];
            	   for(var j = 0;j<group.length; j++){
            		 for(var i = 0;i<data.length; i++){
            		 if(data[i].forward==forward[m]&&data[i].group==group[j]&&xAxis[c]==data[i].name){
            			 xdata.push(data[i].value);
            		 }
            	   }
            		 if (xdata.length==j) {
						xdata.push('-');
					}
            	  }
            	   xData.push(xdata);
            	 }
             }
             
           return {group: group, xAxis: xAxis,xData:xData,xtotal:xtotal}; 
        },
        GetLjData: function (data) {
       	 var group = [];
       	 var xAxis1 = [];
       	 var xAxis2 = [];
         var codes= [];
            var pavementLength = [];
            var xData = [];
            var xtotal =[];
            var map = new Map();
            for (var i = 0; i < data[0].length; i++) {
                for (var j = 0; j < xAxis1.length && xAxis1[j] != data[0][i].name; j++);
                if (j == xAxis1.length)
               	 xAxis1.push(data[0][i].name);
                for (var k = 0; k < group.length && group[k] != data[0][i].group; k++);
                if (k == group.length)
               	 group.push(data[0][i].group);
                for (var m = 0; m < codes.length && codes[m] != data[0][i].code; m++);
                if (m == codes.length)
                	codes.push(data[0][i].code);
            }
            
            for (var i = 0; i < data[1].length; i++) {
           	 for (var j = 0; j < xAxis2.length && xAxis2[j] != data[1][i].name; j++);
                if (j == xAxis2.length)
               	 xAxis2.push(data[1][i].name);
            }
            
        	for(var m = 0; m < xAxis2.length;m++){
       	for (var j = 0; j < data[1].length; j++) {
       			if(xAxis2[m] ==data[1][j].name){
       				pavementLength.push(data[1][j].value);
       			}
       		}
       	}
        	for (var i = 0; i < xAxis2.length; i++) {
           	var temp = [];
           	var hdata = []
           	var total=0;
           	for(var m = 0; m < group.length;m++){
           	for (var j = 0; j < data[0].length; j++) {
           			if((group[m]==data[0][j].group)&&(xAxis2[i] ==data[0][j].name)){
           				temp.push(data[0][j].value);
           				total+=parseFloat(data[0][j].value);
           			}
           		}
           	if (temp.length==m) {
					temp.push(0);
				}
           	}
           	xData.push(temp);
           	xtotal.push(fomatFloat(total));
        	}
        	
        	
        	var ytemp = [];
        	for(var m = 0; m < group.length;m++){
        		var ytotal = 0;
           	for (var j = 0; j < data[0].length; j++) {
           			if((group[m]==data[0][j].group)){
           				ytotal+=parseFloat(data[0][j].value);
           			}
           		}
           	ytemp.push(fomatFloat(ytotal));
           	}
        	 xData.push(ytemp);
        	 xAxis2.push("总计");
        	 pavementLength.push(fomatFloat(sumArray(pavementLength)));
        	 xtotal.push(fomatFloat(sumArray(xtotal)));
        	 return {group: group, xAxis: xAxis2,length:pavementLength,xData:xData,xtotal:xtotal,codes:codes, xAxis1: xAxis1};
       },
        GetXAndGroupData: function (data,type) {
        	var xAxis = [];
            var group = [];
            var result =[];
            for (var i = 0; i < data.length; i++) {
                for (var j = 0; j < xAxis.length && xAxis[j] != data[i].name; j++);
                if (j == xAxis.length)
                    xAxis.push(data[i].name);
                for (var k = 0; k < group.length && group[k] != data[i].group; k++);
                if (k == group.length)
                    group.push(data[i].group);
            }
                for (var i = 0; i < group.length; i++) {
            	var temp = [];
            	for(var m = 0; m < xAxis.length;m++){
            	for (var j = 0; j < data.length; j++) {
            			if((group[i]==data[j].group)&&(xAxis[m] ==data[j].name)){
            				temp.push(data[j].value);
            			}
            		}
            	if (temp.length==m) {
					temp.push(0);
				}
            	}
                result.push(temp);
            }
            return {category: group, xAxis: xAxis,result:result};
        },
        GetHighChartGroupData: function (data,type) {
        	var xAxis = [];
            var group = [];
            var result =[];
            var series = [];
            for (var i = 0; i < data.length; i++) {
                for (var j = 0; j < xAxis.length && xAxis[j] != data[i].name; j++);
                if (j == xAxis.length)
                    xAxis.push(data[i].name);
                for (var k = 0; k < group.length && group[k] != data[i].group; k++);
                if (k == group.length)
                    group.push(data[i].group);
            }
                for (var i = 0; i < group.length; i++) {
            	var temp = [];
            	var st="before";
            	for(var m = 0; m < xAxis.length;m++){
            	for (var j = 0; j < data.length; j++) {
            			if((group[i]==data[j].group)&&(xAxis[m] ==data[j].name)){
            				temp.push(Number(data[j].value));
            			}
            		}
            	if (temp.length==m) {
					temp.push(0);
				}
            	}
            	if(i>3){
            		st = 'last';
            	}
            	var series_temp = { name: group[i], data: temp};
            	series_temp = $.extend({}, { stack: st }, series_temp);
            	series.push(series_temp);
            }
            return {category: group, xAxis: xAxis,series:series};
        },
        GetYAndGroupData: function (data,type) {
        	var xAxis = [];
            var group = [];
            var result =[];
            for (var i = 0; i < data.length; i++) {
                for (var j = 0; j < xAxis.length && xAxis[j] != data[i].name; j++);
                if (j == xAxis.length)
                    xAxis.push(data[i].name);
                for (var k = 0; k < group.length && group[k] != data[i].group; k++);
                if (k == group.length)
                    group.push(data[i].group);
            }
//   
            	for(var m = 0; m < xAxis.length;m++){
            		var temp = [];
            	for (var j = 0; j < data.length; j++) {
            			if(xAxis[m] ==data[j].name){
            				temp.push(data[j].value);
            			}
            		}
            	result.push(temp);
            	}
            return {category: group, xAxis: xAxis,result:result};
        },
        GetGroupData: function (data) {   //便于雷达图数据解析
        	var xAxis = [];
            var group = [];
            var result =[];
            var series = [];
            var radarseries = [];
            var dataJson = [];
            for (var i = 0; i < data.length; i++) {
                for (var j = 0; j < xAxis.length && xAxis[j] != data[i].name; j++);
                if (j == xAxis.length)
                    xAxis.push(data[i].name);
                for (var k = 0; k < group.length && group[k] != data[i].group; k++);
                if (k == group.length)
                    group.push(data[i].group);
            }
            for(var m = 0; m < xAxis.length;m++){
            	var temp = [];
            for (var i = 0; i < group.length; i++) {
            	for (var j = 0; j < data.length; j++) {
            			if((group[i]==data[j].group)&&(xAxis[m] ==data[j].name)){
            				temp.push(Number(data[j].value));
            			}
            		}
            	if (temp.length==i) {
					temp.push(0);
				}
            	}
            dataJson.push(temp);
            var series_temp = { name: xAxis[m], value: temp};
            series.push(series_temp);
            var series_radar = { name: xAxis[m], data: temp};
            radarseries.push(series_radar);
            }
            for(var i=0;i<group.length;i++){
            	var indicator = { name: group[i], max: data[0].max};
            	result.push(indicator);
            }
            return {category: group, xAxis: xAxis,result:result,series: series,dataJson:dataJson,radarseries:radarseries};
        }
	},
        ChartOptionTemplates: {
            CommonOption: {//通用的图表基本配置
                tooltip: {
                    trigger: 'axis'//tooltip触发方式:axis以X轴线触发,item以每一个数据项触发
                },
                toolbox: {
                    show: true, //是否显示工具栏
                    feature: {
                    	mark : { show : true },
                        dataView: { show : false,readOnly: true }, //数据预览
                        restore : {show : false},  
                        saveAsImage : { show : true} 
                    }
                }
            },
            CommonLineOption: {//通用的折线图表的基本配置
                tooltip: {
                    trigger: 'axis',
//                    axisPointer:{
//                        type : 'shadow',
//                        shadowStyle: {
//                            color: 'rgba(150,150,150,0.3)',
//                            width: 'auto',
//                            type: 'default'
//                        }
//                    }
                },
                toolbox: {
                    show: true,
                    feature: {
                    	mark : { show : true },
                        dataView: { show : false,readOnly: true }, //数据预览
                        restore : {show : false},  
                        saveAsImage : { show : true},
//                        magicType : {  
//                            show : true,  
//                            type : ['line', 'bar' ]  //支持柱形图和折线图的切换
//                        }
                    }
                }
            },
            CommonBarsOption: {//通用的折线图表的基本配置
                tooltip: {
                    trigger: 'axis',
                    axisPointer:{
                        type : 'shadow',
                        shadowStyle: {
                            color: 'rgba(150,150,150,0.3)',
                            width: 'auto',
                            type: 'default'
                        }
                    }
                },
                toolbox: {
                    show: true,
                    feature: {
                    	mark : { show : true },
                        dataView: { show : false,readOnly: true }, //数据预览
                        restore : {show : false},  
                        saveAsImage : { show : true},
//                        magicType : {  
//                            show : true,  
//                            type : ['line', 'bar' ]  //支持柱形图和折线图的切换
//                        }
                    }
                }
            },
            CommonPieOption:{ //通用饼状图表的基本配置
            	 tooltip : {
            	        trigger: 'item',
            	        formatter: "{a} <br/>{b} : {c} ({d}%)"
            	    },
            	 toolbox: {
            	        show : true,
            	        feature : {
            	            mark : {show: true},
            	            dataView : {show: true, readOnly: false},
            	            magicType : {
            	                show: true, 
            	                type: ['pie', 'funnel'],
            	                option: {
            	                    funnel: {
            	                        x: '25%',
            	                        width: '50%',
            	                        funnelAlign: 'left',
            	                        max: 1548
            	                    }
            	                }
            	            },
            	            restore : {show: true},
            	            saveAsImage : {show: true}
            	        }
            	    }
            },
            Pie: function (data,title,subtitle,name) {//data:数据格式：{name：xxx,value:xxx}...
                var pie_datas = ECharts.ChartDataFormate.FormateNOGroupData(data);
                var option = {
                	color:colors,
            		title : {
                        text: title || '',
                        subtext: subtitle || '',
                        x:'center'
                    },
                    legend: {
                        orient: 'vertical',
                        x: 'left',
                        y:'center',
                        data: pie_datas.category
                    },
                    calculable : true,
                    series: [{
                            name: name || "",
                            type: 'pie',
                            radius: '40%',
                            center: ['50%', '50%'],
                            data: pie_datas.data
                        }
                    ]
                };
                return $.extend({}, ECharts.ChartOptionTemplates.CommonOption, option);
            },
     Lines: function (data,title, name, is_stack) { //data:数据格式：{name：xxx,group:xxx,value:xxx}...
                var stackline_datas = ECharts.ChartDataFormate.FormateGroupData(data, 'line', is_stack);
                var option = {
                		color:colors,
                		title : {
                            text: title || '',
                            x:'center',
                            y:'top'
                        },
                    legend: {
                    	y:'6%',
                    	x:'center',
                        data: stackline_datas.category,
                    },
                    grid: {
                        left: '5%',
//                        right: '9%',
//                        bottom: '2%',
//                    	  top:'10%',
//                        containLabel: true
                    },
                    xAxis: [{
                    	x:'center',
                        type: 'category', //X轴均为category，Y轴均为value
                        data: stackline_datas.xAxis
//                        boundaryGap: false//数值轴两端的空白策略
                    }],
                    yAxis: [{
                        name: name || '',
                        type: 'value',
                        splitArea: { show: true }
                    }],
                    series: stackline_datas.series
                };
                return $.extend({}, ECharts.ChartOptionTemplates.CommonLineOption, option);
            },
    Bars: function (data,title,subtitle, name, is_stack) {//data:数据格式：{name：xxx,group:xxx,value:xxx}...
                var bars_dates = ECharts.ChartDataFormate.FormateGroupData(data, 'bar', is_stack);
                var option = {
                	color:colors,
            		title : {
                        text: title || '',
                        subtext: subtitle || '',
                        x:'center'
                    },
                    legend: {
                    	y:'8%',
                    	x:'center',
                    	data:bars_dates.category
                    },
                    xAxis: [{
                        type: 'category',
                        data: bars_dates.xAxis,
                        axisLabel: {
                            show: true,
                            interval: 'auto', //默认隐藏显示不下
                            rotate: 0,
                            margion: 8
                        }
                    }],
                    yAxis: [{
                        type: 'value',
                        name: name || '',
                        splitArea: { show: true }
                    }],
                    series: bars_dates.series
                };
                return $.extend({}, ECharts.ChartOptionTemplates.CommonBarsOption, option);
            },
      //其他的图表配置，如雷达图
    Radar:function(data,title,subtitle, name,is_stack){
    	var radar_dates = ECharts.ChartDataFormate.FormateGroupData(data, 'radar', is_stack);
    	var option ={ 
    		color:colors,
    		title : {
            text: title|| '',
            subtext: subtitle || ''
        },
        tooltip : {
            trigger: 'axis'
        },
        legend: {
            x : 'center',
            data:radar_dates.category,
            itemStyle: {
                normal: {
                	color:'black'
                }
            }
        },
        toolbox: {
            show : true,
            feature : {
                mark : {show: true},
                dataView : {show: true, readOnly: false},
                restore : {show: true},
                saveAsImage : {show: true}
            }
        },
        calculable : true,
        polar : [
            {
                indicator : radar_dates.indicator,
                radius : 130
            }
        ],
        series : [
            {
            	name: name,
                type: 'radar',
                itemStyle: {
                    normal: {
                        areaStyle: {
                            type: 'default'
                        }
                    }
                },
                data : radar_dates.series
            }
        ]
    	}
     return $.extend({},  option);
    }      
    },
	LoadChart:function(id,option){
		echarts.init($("#"+id)[0]).setOption(option);
		return echarts.init($("#"+id)[0]);
	},
	GetEcharts:function(id){
		return echarts.init($("#"+id)[0]);
	}
}

